package mamikeji.liaowa.utils;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.TextView;

import com.squareup.okhttp.Request;
import com.wang.avi.AVLoadingIndicatorView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.FileCallBack;

import java.io.File;

import mamikeji.liaowa.widget.roundvideo.MyVideoView;

/**
 * Created by 廉冲-android开发 on 2017/7/28.
 */
public class DownLoadVideo {
    private static File mFile;
    private static boolean bool = false;

    public static boolean creatFile(String videoName, String src) {
        if (mFile == null) {
            mFile = new File(src);
        }
        //判断文件夹是否存在，如果不存在就创建，否则不创建
        if (!mFile.exists()) {
            //通过file的mkdirs()方法创建<span style="color:#FF0000;">目录中包含却不存在</span>的文件夹
            mFile.mkdirs();
        }

        //新建一个File类型的成员变量，传入文件名路径。
        File file = new File(src + videoName);
        //判断文件是否存在，存在就删除
        if (file.exists()) {
            return false;
        } else {
            return true;
        }

    }

    public static void downVido(final String video_url, final MyVideoView videoView, final AVLoadingIndicatorView avi_video) {
        final String[] videoName = video_url.split("/");
        avi_video.show();
        if (creatFile(videoName[videoName.length - 1], "/sdcard/liaowa/liaoVideo")) {
            OkHttpUtils.get()
                    .url(video_url)
                    .build()
                    .execute(new FileCallBack("/sdcard/liaowa/liaoVideo", videoName[videoName.length - 1]) {
                        @Override
                        public void inProgress(float progress) {
                            ForLog.i("下载进度", progress + "");
                        }

                        @Override
                        public void onError(Request request, Exception e) {
                            avi_video.hide();
                            videoView.setVideoURI(Uri.parse(video_url));
                            videoView.start();
                        }

                        @Override
                        public void onResponse(File file) {
                            avi_video.hide();
                            videoView.setVideoURI(Uri.parse("/sdcard/liaowa/liaoVideo/" + videoName[videoName.length - 1]));
                            videoView.start();
                        }
                    });
        } else {
            avi_video.hide();
            videoView.setVideoURI(Uri.parse("/sdcard/liaowa/liaoVideo/" + videoName[videoName.length - 1]));
            videoView.start();
        }
    }

    public static void downJPG(final String video_url, final MyVideoView videoView, final AVLoadingIndicatorView avi_video) {
        final String[] videoName = video_url.split("/");
        avi_video.show();
        if (creatFile(videoName[videoName.length - 1], "/sdcard/liaowa/liaoVideo")) {
            OkHttpUtils.get()
                    .url(video_url)
                    .build()
                    .execute(new FileCallBack("/sdcard/liaowa/liaoVideo", videoName[videoName.length - 1]) {
                        @Override
                        public void inProgress(float progress) {
                            ForLog.i("下载进度", progress + "");
                        }

                        @Override
                        public void onError(Request request, Exception e) {
                            avi_video.hide();
                            videoView.setVideoURI(Uri.parse(video_url));
                            videoView.start();
                        }

                        @Override
                        public void onResponse(File file) {
                            avi_video.hide();
                            videoView.setVideoURI(Uri.parse("/sdcard/liaowa/liaoVideo/" + videoName[videoName.length - 1]));
                            videoView.start();
                        }
                    });
        } else {
            avi_video.hide();
            videoView.setVideoURI(Uri.parse("/sdcard/liaowa/liaoVideo/" + videoName[videoName.length - 1]));
            videoView.start();
        }
    }

    public static void downAPK(final Context context, final String APK_url, String name, final TextView textView) {
        if (creatFile("liaowa_" + name + ".apk", "/sdcard/liaowa/liaoAPK")) {
            OkHttpUtils.get()
                    .url(APK_url)
                    .build()
                    .execute(new FileCallBack("/sdcard/liaowa/liaoAPK", "liaowa_" + name + ".apk") {
                        @Override
                        public void inProgress(float progress) {
                            ForLog.i("下载进度", progress + "");
                            float b = (float) (Math.round(progress * 100));
                            String num = b + "%";
                            String text = b == 100 ? "下载完成！" : "下载进度："+num;
                            textView.setText(text);
                        }

                        @Override
                        public void onError(Request request, Exception e) {
                        }

                        @Override
                        public void onResponse(File file) {

                            Intent intent = new Intent();
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.setAction(Intent.ACTION_VIEW);
                            intent.setDataAndType(Uri.fromFile(file),
                                    "application/vnd.android.package-archive");
                            context.startActivity(intent);
                        }
                    });
        }
    }
}
